package com.zmzncs.lmtc.module.customer.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmzncs.lmtc.common.pojo.enums.OrderPayTypeEnum;
import com.zmzncs.lmtc.common.pojo.enums.WhetherEnum;
import com.zmzncs.lmtc.common.util.*;
import com.zmzncs.lmtc.module.customer.dao.*;
import com.zmzncs.lmtc.module.customer.pojo.car.CarDTO;
import com.zmzncs.lmtc.module.customer.pojo.card.Card;
import com.zmzncs.lmtc.module.customer.pojo.card.CardDTO;
import com.zmzncs.lmtc.module.customer.pojo.card.CardHasForm;
import com.zmzncs.lmtc.module.customer.service.CardService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;

/**
 * <p>
 * 客户-会员卡 服务实现类
 * </p>
 *
 * @author 任建波
 * @since 2020-09-02
 */
@Slf4j
@Service
public class CardServiceImpl extends ServiceImpl<CardDao, Card> implements CardService {

    @Autowired
    CardDao cardDao;
    @Autowired
    CarDao carDao;


    /**
     * 是否有会员卡
     */
    @Override
    public WhetherEnum hasCustomerCard(CardHasForm cardHasForm) {
        //  根据车牌号获取客户车辆
        CarDTO carDTO = carDao.getCustomerCarByCarNumber(cardHasForm.getCustomerId(), cardHasForm.getCarNumber());
        if(carDTO == null){
            ExceptionUtil.businessException("车牌" + cardHasForm.getCarNumber() + "不存在");
        }
        //  根据carId和parkId查询客户车辆停车场月租服务
        List<CardDTO> monthCardDTOList = cardDao.getCustomerMonthCardByCarIdAndParkId(cardHasForm.getCustomerId(), carDTO.getId(), cardHasForm.getParkId());
       // log.info("用户："+cardHasForm.getCustomerId()+"车场："+cardHasForm.getParkId()+"月租会员卡:"+monthCardDTOList.size());
        //  月租会员卡
        if(monthCardDTOList.size() > 0){
            return WhetherEnum.YES;
        }
        //  根据carId和parkId查询客户车辆停车场时长服务
        List<CardDTO> durationCardDTOList = cardDao.getCustomerDurationCardByCarIdAndParkId(cardHasForm.getCustomerId(), carDTO.getId(), cardHasForm.getParkId());
      //  log.info("用户："+cardHasForm.getCustomerId()+"车场："+cardHasForm.getParkId()+"时长会员卡:"+durationCardDTOList.size());
        //  时长会员卡
        if(durationCardDTOList!=null && durationCardDTOList.size() > 0){
            return WhetherEnum.YES;
        }
        return WhetherEnum.NO;
    }



    /**
     * 是否有时长会员卡
     */
    @Override
    public OrderPayTypeEnum hasCustomerCardAndUpdate(CardHasForm cardHasForm) {
        //  根据车牌号获取客户车辆
        CarDTO carDTO = carDao.getCustomerCarByCarNumber(cardHasForm.getCustomerId(), cardHasForm.getCarNumber());
        if(carDTO == null){
            ExceptionUtil.businessException("车牌" + cardHasForm.getCarNumber() + "不存在");
        }
        //  根据carId和parkId查询客户车辆停车场月租服务
        List<CardDTO> monthCardDTOList = cardDao.getCustomerMonthCardByCarIdAndParkId(cardHasForm.getCustomerId(), carDTO.getId(), cardHasForm.getParkId());
        //  月租会员卡
        if(monthCardDTOList.size() > 0){
            return OrderPayTypeEnum.MONTH;
        }

        long diff = cardHasForm.getEndTime().getTime() - cardHasForm.getBeginTime().getTime();
        long day = diff / (24 * 60 * 60 * 1000);    // 差多少天
        long hour = (diff / (60 * 60 * 1000) - day * 24);   // 差多少小时
        long min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);  // 差多少分钟

        //  根据carId和parkId查询客户车辆停车场时长服务
        List<CardDTO> durationCardDTOList = cardDao.getCustomerDurationCardByCarIdAndParkId(cardHasForm.getCustomerId(), carDTO.getId(), cardHasForm.getParkId());
        if(durationCardDTOList==null || durationCardDTOList.size()==0){
            return OrderPayTypeEnum.CASH;
        }
        for(CardDTO cardDTO: durationCardDTOList){
            //  如果卡剩余时长大于停车时长或者是最后一张卡，扣除时长，结束循环
            if(cardDTO.getTimeLeft() - min >= 0 || cardDTO.getId().intValue() == durationCardDTOList.get(durationCardDTOList.size()-1).getId()){
                Card card = new Card();
                //  如果不够一个小时，按一个小时收费
                card.setTimeLeft((int)Math.ceil(hour) == 0 ? 60 : (int)Math.ceil(hour) * 60);
                cardDao.updateById(card);
                return OrderPayTypeEnum.DURATION;
            } else {
                //  如果卡剩余时长小于停车时长，将卡时长设置为0
                Card card = new Card();
                //  如果不够一个小时，按一个小时收费
                card.setTimeLeft(0);
                cardDao.updateById(card);

                //  扣除卡的剩余时间
                diff = diff - cardDTO.getTimeLeft() * 60 * 1000;
                day = diff / (24 * 60 * 60 * 1000);    // 差多少天
                hour = (diff / (60 * 60 * 1000) - day * 24);   // 差多少小时
                min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);  // 差多少分钟
            }
        }
        return OrderPayTypeEnum.DURATION;
    }

    /**
     * 是否有月租卡
     *
     * @param carNumber
     */
    @Override
    public CarDTO getValidCarByCarNumber(String carNumber) {
        return carDao.getValidCarByCarNumber(carNumber);
    }

}
